package com.cloud.encrypting_cloud_storage.interceptor;

import com.cloud.encrypting_cloud_storage.entity.User;
import com.cloud.encrypting_cloud_storage.enums.StatusEnum;
import com.cloud.encrypting_cloud_storage.exceptions.PageException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author leon
 * @Description: 权限认证
 * @date 2022年04月22日 10:49
 */
@Slf4j
public class SimpleAuthenticationInterceptor implements HandlerInterceptor {
    @Value("${user.key}")
    private String userKey;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User sessionUser = (User)session.getAttribute(userKey);
        if(sessionUser==null){
            throw new PageException(StatusEnum.FORBIDDEN);
        }

        return true;
    }


}
